package com.hy.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hy.base.DozerBase;
import com.hy.dao.GazetteerMapper;
import com.hy.dao.GazetteerMapper_My;
import com.hy.exception.ExceptionEnum;
import com.hy.exception.ServiceException;
import com.hy.form.input.GazetteerAddForm;
import com.hy.form.query.GazetteerQueryForm;
import com.hy.form.result.GazetteerResultForm;
import com.hy.model.Gazetteer;
import com.hy.model.GazetteerExample;
import com.hy.service.IGazetteerService;

@Service
public class GazetteerServiceImpl extends DozerBase implements IGazetteerService {
	
	static final Logger logger = LoggerFactory.getLogger(GazetteerServiceImpl.class);

	@Autowired
	private GazetteerMapper gazetteerMapper;
	
	@Autowired
	private GazetteerMapper_My gazetteerMapper_My;
	
	@Override
	public void add(GazetteerAddForm gazetteerAddForm) {
		Gazetteer gazetteer = this.getDozerMapper().map(gazetteerAddForm, Gazetteer.class);
		
		String id = gazetteer.getId();
		if(StringUtils.isEmpty(id)){
			throw new ServiceException(ExceptionEnum.ID_NOT_EMPTY);
		}
		
		GazetteerExample gazetteerExample = new GazetteerExample();
		gazetteerExample.or().andParent_idEqualTo(id);
		int count = gazetteerMapper.countByExample(gazetteerExample);
		if(count > 0){
			throw new ServiceException(ExceptionEnum.ID_REPEAT);
		}
		
		Date date = new Date();
		gazetteer.setCreate_time(date);
		gazetteer.setUpdate_time(date);
		gazetteerMapper.insertSelective(gazetteer);	

	}

	@Override
	public void update(GazetteerAddForm gazetteerAddForm) {
		Gazetteer gazetteer = this.getDozerMapper().map(gazetteerAddForm, Gazetteer.class);
		
		String id = gazetteer.getId();
		if(StringUtils.isEmpty(id)){
			throw new ServiceException(ExceptionEnum.ID_NOT_EMPTY);
		}
		
		GazetteerExample gazetteerExample = new GazetteerExample();
		gazetteerExample.or().andParent_idEqualTo(id);
		int count = gazetteerMapper.countByExample(gazetteerExample);
		if(count == 0){
			throw new ServiceException(ExceptionEnum.RECORD_NOT_EXIST);
		}
		
		Date date = new Date();
		gazetteer.setUpdate_time(date);
		gazetteerMapper.updateByPrimaryKey(gazetteer);
	}

	@Override
	public Gazetteer selectByPrimaryKey(String id) {
		
		if(StringUtils.isEmpty(id)){
			throw new ServiceException(ExceptionEnum.ID_NOT_EMPTY);
		}
		
		Gazetteer gazetteer = gazetteerMapper.selectByPrimaryKey(id);
		if(gazetteer == null) {
			throw new ServiceException(ExceptionEnum.RECORD_NOT_EXIST);
		}
		return gazetteer;
	}

	@Override
	public void del(String id) {
		if(StringUtils.isEmpty(id)){
			throw new ServiceException(ExceptionEnum.ID_NOT_EMPTY);
		}
		
		gazetteerMapper.deleteByPrimaryKey(id);
	}

	@Override
	public PageInfo<GazetteerResultForm> page(GazetteerQueryForm gazetteerQueryForm) {
		logger.info("page start");
		Map<String, Object> params = new HashMap<String, Object>();
		//中文名称
        if(StringUtils.isNotEmpty(gazetteerQueryForm.getName())){
            params.put("name", gazetteerQueryForm.getName());
        }
        //英文名称
        if(StringUtils.isNotEmpty(gazetteerQueryForm.getEn_name())){
            params.put("en_name", gazetteerQueryForm.getEn_name());
        }
        if(gazetteerQueryForm.getPage_index() != null && gazetteerQueryForm.getPage_size() != null){
            PageHelper.startPage(gazetteerQueryForm.getPage_index(), gazetteerQueryForm.getPage_size());
        }
        List<GazetteerResultForm> list = gazetteerMapper_My.queryGazetteers(params);
		logger.info("page end");
		return new PageInfo<GazetteerResultForm>(list);
	}

}
